﻿#region 命名空间

using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.Web.Controllers
{
    [AutomaticTemplate]
    public class UserController : Controller
    {

        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                ViewName = "Index",
                Model = new UserIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(StoreRequestParameters parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                ExtPagingFeature ff = new ExtPagingFeature(parameters);

                var pageInfo = proxy.Channel.GetUsersPaged(ff.Parameter,null);

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        // GET: Manage/User/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserDTO userDTO = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                userDTO = proxy.Channel.GetUserByKey(id.Value).Data;
            }
            if (userDTO == null)
            {
                return HttpNotFound();
            }
            userDTO.Password = string.Empty;
            return new PartialViewResult { Model = userDTO.To<UserModel>() };
        }

        [Display(Name = "新增")]
        public ActionResult Create()
        {
            return new PartialViewResult { Model = new UserModel() };
        }


        [Display(Name = "编辑")]
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserDTO userDTO = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                userDTO = proxy.Channel.GetUserByKey(id.Value).Data;
            }
            if (userDTO == null)
            {
                return HttpNotFound();
            }

            userDTO.Password = string.Empty;
            return new PartialViewResult { Model = userDTO.To<UserModel>() };
        }


        [HttpPost]
        public ActionResult Save(UserModel user)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                UserDTO userDTO = user.To<UserDTO>();
                if (userDTO.Id.IsEmpty())
                {
                    proxy.Channel.CreateUser(userDTO);
                }
                else
                {
                    proxy.Channel.UpdateUser(userDTO);
                }
            }

            return this.FormPanel();
        }

        [HttpPost]
        [Display(Name = "删除")]
        public JsonResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IUserService>())
            {
                res.Data = proxy.Channel.DeleteUsers(ids);
            }

            return res;
        }
    }
}